<template>
	<view class="my-info">
		<image :src="mineImage" class="my-info-logo" mode="aspectFit" id="image-logo"
			style="height:60px;margin: 10px auto;">
		</image>
		<view class="my-info-name">
			<text style="font-size: 15px">{{name}}</text>
		</view>
		<view class="my-info-change">
			<view style="display: flex;font-size: 13px;align-items: center;">
				<image src="../../static/tabs/editChange.png" alt style="width: 17px;height: 17px;"></image>
				<text @click="changeInfo">修改资料</text>
			</view>
		</view>
	</view>
	
	
	<view class="container">
	
		<view class="uni-padding-wrap">
			<uni-segmented-control :current="current" :values="items" style-type="text" :active-color="activeColor" @clickItem="onClickItem" />
		</view>
		
		

		<view v-if="current == 0">
			<view class="day-schedule" style="margin:0px auto ;">
				<uni-card title="今日日程">
					<uni-list v-for="item in dailyScheduleList">
						<uni-list-item class="day-schedule-item" :title='item.title' :note="item.beginTime+'-'+item.endTime" :rightText="item.location"
							 :to="'/pages/mine/checkSchedule?id='+item.id"  ></uni-list-item>
					</uni-list>
				</uni-card>
				<uni-card title="当周日程" sub-title="点击进入当周日程" @click="onClickWeek">
				</uni-card>
				<uni-card title="所有日程" sub-title="点击进入所有日程" @click="onClick">
				</uni-card>
			</view>
		</view>


		<view v-if="current == 1">
			<uni-row class="demo-uni-row" :width="nvueWidth">
				<uni-col :span="19">
					<view class="demo-uni-col"><uni-search-bar placeholder="搜索我参与的活动" bgColor="#EEEEEE" @confirm="partSearch" v-model="partSearchValue" /></view>
				</uni-col>
				<uni-col :span="5">
					<view class="demo-uni-col"><button style="margin-top: 25rpx;" type="primary" plain="true" size="mini" @click="partConditionSearch">确认</button></view>
				</uni-col>
			</uni-row>
			<uni-row class="demo-uni-row" :width="nvueWidth">
				<uni-col :span="8">
					<view class="demo-uni-col"><uni-data-select v-model="partValue1" :localdata="partRange1" :clear="true" placeholder="进行中"></uni-data-select></view>
				</uni-col>
				<uni-col :span="8">
					<view class="demo-uni-col"><uni-data-select v-model="partValue2" :localdata="partRange2" :clear="true" placeholder="所有类别"></uni-data-select></view>
				</uni-col>
				<uni-col :span="8">
					<view class="demo-uni-col"><uni-data-select v-model="partValue3" :localdata="partRange3" :clear="true" placeholder="最近发生"></uni-data-select></view>
				</uni-col>
			</uni-row>

			<uni-card v-for="part in partList" :title="part.name" spacing="5px 5px" :sub-title="part.place" :extra="`${part.beginTime}\n${part.endTime}`" :thumbnail="part.pic1">
				<view class="ml-20 mb-20" @click="goDetail(part.id)" >
					<text>{{ part.info }}</text>
				</view>
				<view class="mb-20"><uni-steps :options="part.list" :active="part.leastUnsuccessfulStage" /></view>
				<uni-row class="demo-uni-row" :width="nvueWidth">
					<uni-col :span="4"><view class="demo-uni-col"></view></uni-col>
					<uni-col :span="6">
						<view class="demo-uni-col"><button type="primary" plain="true" size="mini" @click="delPart(part.id)">删除</button></view>
					</uni-col>
					<uni-col :span="8">
						<view class="demo-uni-col">
							<navigator :url="`/pages/sign-history/sign-history?activityId=${part.id}`"><button type="primary" plain="true" size="mini">历史签到</button></navigator>
						</view>
					</uni-col>
					<uni-col :span="6">
						<view class="demo-uni-col">
							<navigator :url="`/pages/sign/sign?activityId=${part.id}&activityName=${part.name}&uid=${uid}`">
								<button type="primary" plain="true" size="mini" @click="">签到</button>
							</navigator>
						</view>
					</uni-col>
				</uni-row>
			</uni-card>
			<uni-load-more iconType="circle" :status="partStatus" @clickLoadMore="getMorePart" />
		</view>

		<view v-if="current == 2">
			<uni-row class="demo-uni-row" :width="nvueWidth">
				<uni-col :span="19">
					<view class="demo-uni-col"><uni-search-bar placeholder="搜索我发布的活动" bgColor="#EEEEEE" @confirm="hostSearch" v-model="hostSearchValue" /></view>
				</uni-col>
				<uni-col :span="5">
					<view class="demo-uni-col"><button style="margin-top: 25rpx;" type="primary" plain="true" size="mini" @click="hostConditionSearch">确认</button></view>
				</uni-col>
			</uni-row>

			<uni-row class="demo-uni-row" :width="nvueWidth">
				<uni-col :span="6">
					<view class="demo-uni-col"><uni-data-select v-model="hostValue1" :localdata="hostRange1" :clear="true" placeholder="进行中"></uni-data-select></view>
				</uni-col>
				<uni-col :span="6">
					<view class="demo-uni-col"><uni-data-select v-model="hostValue2" :localdata="hostRange2" :clear="true" placeholder="所有类别"></uni-data-select></view>
				</uni-col>
				<uni-col :span="6">
					<view class="demo-uni-col"><uni-data-select v-model="hostValue3" :localdata="hostRange3" :clear="true" placeholder="最近发生"></uni-data-select></view>
				</uni-col>
				<uni-col :span="6">
					<view class="demo-uni-col"><uni-data-select v-model="hostValue4" :localdata="hostRange4" :clear="true" placeholder="已发布"></uni-data-select></view>
				</uni-col>
			</uni-row>
			<uni-card v-for="host in hostList" :title="host.name" spacing="5px 5px" :sub-title="host.place" :thumbnail="host.pic1" >
				<view class="uni-body bottom-margin" @click="goDetail(host.id)" >
					<text>{{ host.info }}</text>
				</view>
				<view class="bottom-margin"><uni-steps :options="hostTitleList" :active="host.active" /></view>
				<uni-row class="demo-uni-row" :width="nvueWidth">
					<uni-col :span="6">
						<view class="demo-uni-col">
							<navigator :url="`/pages/host-sign-list/host-sign-list?activityId=${host.id}&activityName=${host.name}&maxStage=${host.stageCount}`">
								<button type="primary" plain="true" size="mini">签到</button>
							</navigator>
							<!-- 包括名单、签到数据统计、已读数据统计等 -->
						</view>
					</uni-col>
					<uni-col :span="6">
						<view class="demo-uni-col">
							<navigator :url="`/pages/host-reminder-list/host-reminder-list?activityId=${host.id}&activityName=${host.name}`"><button type="primary" plain="true" size="mini">提醒</button></navigator>
							<!-- 包括发送提醒、发布签到 -->
						</view>
					</uni-col>
					<uni-col :span="6">
						<view class="demo-uni-col">
							<navigator :url="`/pages/member-list/member-list?activityId=${host.id}&activityName=${host.name}`">
								<button type="primary" plain="true" size="mini">名单</button>
							</navigator>
						</view>
					</uni-col>
					<uni-col :span="6">
						<view class="demo-uni-col"><button type="primary" plain="true" size="mini" @click="delHost(host.id)">删除</button></view>
					</uni-col>
				</uni-row>
			</uni-card>
			
			<uni-load-more iconType="circle" :status="hostStatus" @clickLoadMore="getMoreHost" />
		
	</view>
	</view>
	
	<view class="btn-row">
		<button type="primary" class="primary" @tap="bindLogout">退出登录</button>
	</view>
	
</template>

<script>
	function removeDate(date) {
		if (typeof date !== 'string') {
			return
		}
		var n = date.split(" ")
		var result = n[1]
		return result
	};
export default {
	components: {},
	data() {
		return {
			name: '未登录',
			dailyScheduleList: [],
			mineImage: '../../static/tabs/mine.png',
			time: '',
			uid: 3,
			current: 0,
			activeColor: '#007aff',
			items: ['我的日程', '我参与的', '我发布的'],

			partSearchValue: '',
			partStatus: 'more',
			partPageNum: 1,
			partPageSize: 1,
			partList: [],
			partRequestData: {
				orderBy: 'a.id',
				order: 'Desc'
			},
			partValue1: '进行中',
			partRange1: [
				{
					value: '进行中',
					text: '进行中'
				},
				{
					value: '即将开始',
					text: '即将开始'
				},
				{
					value: '已结束',
					text: '已结束'
				},
				{
					value: '所有时间',
					text: '所有时间'
				}
			],
			partValue2: '所有类别',
			partRange2: [
				{
					value: '所有类别',
					text: '所有类别'
				},
				{
					value: '文体',
					text: '文体'
				},
				{
					value: '学术',
					text: '学术'
				},
				{
					value: '社交',
					text: '社交'
				},
				{
					value: '竞赛',
					text: '竞赛'
				}
			],
			partValue3: 0,
			partRange3: [
				{
					value: 0,
					text: '最近发生'
				},
				{
					value: 1,
					text: '最多关注'
				}
			],

			hostSearchValue: '',
			hostStatus: 'more',
			hostPageNum: 1,
			hostPageSize: 1,
			hostList: [],
			hostRequestData: {
				// 由于发布者就是自己,因此不可能出现按照发布者姓名查找的情况
				byUsername: false,
				keyword: '',
				type: '',
				orderBy: '',
				sequence: false
			},
			hostValue1: '进行中',
			hostRange1: [
				{
					value: '进行中',
					text: '进行中'
				},
				{
					value: '即将开始',
					text: '即将开始'
				},
				{
					value: '已结束',
					text: '已结束'
				},
				{
					value: '所有时间',
					text: '所有时间'
				}
			],
			hostValue2: '所有类别',
			hostRange2: [
				{
					value: '所有类别',
					text: '所有类别'
				},
				{
					value: '文体',
					text: '文体'
				},
				{
					value: '学术',
					text: '学术'
				},
				{
					value: '社交',
					text: '社交'
				},
				{
					value: '竞赛',
					text: '竞赛'
				}
			],
			hostValue3: 0,
			hostRange3: [
				{
					value: 0,
					text: '最近发生'
				},
				{
					value: 1,
					text: '最多关注'
				}
			],
			hostValue4: '所有阶段',
			hostRange4: [
				{
					value: '已发布',
					text: '已发布'
				},
				{
					value: '审核中',
					text: '审核中'
				},
				{
					value: '编辑中',
					text: '编辑中'
				},
				{
					value: '所有阶段',
					text: '所有阶段'
				}
			],
			hostTitleList: [
				{
					title: '编辑'
				},
				{
					title: '审核'
				},
				{
					title: '发布'
				}
			],

			apiUrl: 'http://localhost:8080/',
			promptWord: '展开',
			nvueWidth: 730
		};
	},
	onLoad() {

		
		
		this.uid = uni.getStorageSync("uid");
		
		uni.request({
			url:"http://127.0.0.1:8080/user/"+this.uid,
			header: {
				Authorization:"Bearer " + uni.getStorageSync('token')
			},
			success: (res) => {
				console.log(res);
				this.name = res.data.name;
				this.mineImage = res.data.picture;
			}
		})
		
		
		this.partPageNum = 1;
		this.getPart();
		this.hostPageNum = 1;
		this.getHost();
	},
	onPullDownRefresh() {
		this.partPageNum = 1;
		this.getPart();
		this.hostPageNum = 1;
		this.getHost();
		this.dataInit()
		console.log('刷新'),
		setTimeout(() => {
			this.msg = 'refresh',
				uni.stopPullDownRefresh()
		}, 1000)
	},
	onShow(){
		this.partPageNum = 1;
		this.getPart();
		this.hostPageNum = 1;
		this.getHost();
		
		this.dataInit();
		
	},
	methods: {
		bindLogout(){
			/* this.adminName="未登录";
			this.adminPic="";
			this.adminUid=0; */
			uni.clearStorageSync();
			uni.reLaunch({
				url:"/pages/login/login"
			})
		},
		goDetail(id){
			uni.navigateTo({
				url:"/pages/activityDetail/activityDetail?id="+id
			})
		},
		clickPromptWord() {
			if (this.promptWord == '展开') {
				this.promptWord = '收起';
			} else {
				this.promptWord = '展开';
			}
		},
		delHost(id) {
			uni.request({
				method: 'DELETE',
				url: this.apiUrl + 'activity/' + id,
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {},
				success: res => {
					console.log(res.data);
					if (res.statusCode == 200) {
						uni.showToast({
							icon: 'success',
							title: '成功删除'
						});
							this.partPageNum = 1;							this.getPart();							this.hostPageNum = 1;							this.getHost();							this.dataInit();
					}
					if (res.statusCode == 400) {
						uni.showToast({
							icon: 'error',
							title: JSON.parse(res.data).message
						});
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
			uni.startPullDownRefresh();
		},
		delPart(id) {
			uni.request({
				method: 'DELETE',
				url: this.apiUrl + 'entry',
				header: {
					Authorization:"Bearer " +  uni.getStorageSync('token')
				},
				data: {
					activityId: id,
					uid: this.uid
				},
				success: res => {
					console.log(res.data);
					if (res.statusCode == 200) {
						uni.showToast({
							icon: 'success',
							title: '成功删除'
						});
					}
					if (res.statusCode == 400) {
						uni.showToast({
							icon: 'error',
							title: JSON.parse(res.data).message
						});
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
			uni.startPullDownRefresh();
		},
		onClickItem(e) {
			if (this.current !== e.currentIndex) {
				this.current = e.currentIndex;
			}
		},
		partConditionSearch() {
			if (this.partValue2 == '所有类别' || this.partValue2 == '') {
				if (typeof this.partRequestData.type != 'undefined') {
					delete this.partRequestData.type;
				}
			} else {
				this.partRequestData.type = this.partValue2;
			}
			switch (this.partValue3) {
				case 0:
				case '':
					this.partRequestData.orderBy = 'a.id';
					this.partRequestData.sequence = false;
					break;
				case 1:
					this.partRequestData.orderBy = 'a.added_times';
					this.partRequestData.sequence = false;
					break;
			}
			this.partPageNum = 1;
			this.getPart();
		},
		getMorePart() {
			if (this.partStatus == 'more') {
				this.partPageNum++;
				this.getPart();
			}
		},
		partSearch() {
			this.partRequestData.keyword = this.partSearchValue;
			this.partPageNum = 1;
			this.getPart();
		},
		getPart() {
			this.partStatus = 'loading';
			this.partRequestData.uid = this.uid;
			this.partRequestData.partPageNum = this.partPageNum;
			this.partRequestData.partPageSize = this.partPageSize;
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'entry',
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: this.partRequestData,
				success: res => {					
					console.log(res.data);
					if (this.partPageNum <= 1) {
						this.partList = [];
					}
					let originSize = this.partList.length;
					this.partList.push.apply(this.partList, res.data.records);
					
					
					
					
					let deltaSize = res.data.records.length;
					for (let i = originSize; i < originSize + deltaSize; i++) {
						let btimes = this.partList[i].beginTime.split("，")
						let etimes = this.partList[i].endTime.split("，")
						let places = this.partList[i].place.split("，")
						this.partList[i].list = [];
						let stageNames = this.partList[i].stageNames.split("，")
						for (let j = 1; j <= this.partList[i].stageCount; j++) {
							this.partList[i].list.push({
								title: stageNames[j-1]
							});
						}
						if (this.partList[i].leastUnsuccessfulStage == null) {
							this.partList[i].leastUnsuccessfulStage = this.partList[i].stageCount - 1;
						} else {
							this.partList[i].leastUnsuccessfulStage = this.partList[i].leastUnsuccessfulStage - 2;
						}
						this.partList[i].beginTime = btimes[this.partList[i].leastUnsuccessfulStage]
						this.partList[i].endTime = etimes[this.partList[i].leastUnsuccessfulStage]
						this.partList[i].place = places[this.partList[i].leastUnsuccessfulStage]
						
					}
					
					
					
					try {
						uni.setStorageSync('partList', this.partList);
					} catch (e) {
						console.log(e.message);
					}
					this.partList = uni.getStorageSync('partList');

					if (res.data.total <= originSize + deltaSize) {
						this.partStatus = 'noMore';
					} else {
						this.partStatus = 'more';
					}
					uni.stopPullDownRefresh();
				},
				fail: err => {
					this.partPageNum -= 1;
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		hostConditionSearch() {
			if (this.hostValue2 == '所有类别' || this.hostValue2 == '') {
				// if (typeof this.hostRequestData.type != 'undefined') {
				// 	delete this.hostRequestData.type;
				// }
				this.hostRequestData.type = '';
			} else {
				this.hostRequestData.type = this.hostValue2;
			}
			switch (this.hostValue3) {
				case 0:
				case '':
					this.hostRequestData.orderBy = 'a.id';
					this.hostRequestData.sequence = false;
					break;
				case 1:
					this.hostRequestData.orderBy = 'a.added_times';
					this.hostRequestData.sequence = false;
					break;
			}
			switch (this.hostValue4) {
				case '已发布':
				case '':
					this.hostRequestData.state = '00100';
					break;
				case '审核中':
					this.hostRequestData.state = '01000';
					break;
				case '编辑中':
					this.hostRequestData.state = '10000';
					break;
				case '所有阶段':
					if (typeof this.hostRequestData.state != 'undefined') {
						delete this.hostRequestData.state;
					}
					break;
			}
			this.hostPageNum = 1;
			this.getHost();
		},
		getMoreHost() {
			if (this.hostStatus == 'more') {
				this.hostPageNum++;
				this.getHost();
			}
		},
		hostSearch() {
			this.hostRequestData.keyword = this.hostSearchValue;
			this.hostPageNum = 1;
			this.getHost();
		},

		getHost() {
			this.hostStatus = 'loading';
			this.hostRequestData.hostUid = this.uid;
			this.hostRequestData.pageNum = this.hostPageNum;
			this.hostRequestData.pageSize = this.hostPageSize;
			// console.log(uni.getStorageSync('token'));
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'activity',
				header: {
					Authorization: "Bearer " + uni.getStorageSync('token')
				},
				data: this.hostRequestData,
				success: res => {
					console.log(res.data);
					if (this.hostPageNum <= 1) {
						this.hostList = [];
					}
					let originSize = this.hostList.length;
					this.hostList.push.apply(this.hostList, res.data.records);
					let deltaSize = res.data.records.length;
					for (let i = originSize; i < originSize + deltaSize; i++) {
						switch (this.hostList[i].state) {
							case 'editing':
								this.hostList[i].active = 0;
								break;
							case 'checking':
								this.hostList[i].active = 1;
								break;
							case 'released':
								this.hostList[i].active = 2;
								break;
						}
					}

					try {
						uni.setStorageSync('hostList', this.hostList);
					} catch (e) {
						console.log(e.message);
					}
					this.hostList = uni.getStorageSync('hostList');

					if (res.data.total <= originSize + deltaSize) {
						this.hostStatus = 'noMore';
					} else {
						this.hostStatus = 'more';
					}
					uni.stopPullDownRefresh();
				},
				fail: err => {
					this.hostPageNum -= 1;
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		onClickWeek(e){
			if (e == 'title') {
				uni.navigateTo({
					url: "/pages/mine/weekCalendar"
				})
			} else if (e == 'content') {
				console.log('is content')
			}
		},
		changeInfo(){
			uni.navigateTo({
				url:"/pages/mine/userInfo?uid="+this.uid
			})
		},
		onClick(e) {
			if (e == 'title') {
				uni.navigateTo({
					url: "/pages/mine/calendar"
				})
			} else if (e == 'content') {
				console.log('is content')
			}
		},
		actionsClick(text) {
			uni.showToast({
				title: text,
				icon: 'none'
			})
		},
		dataInit() {
			console.log('初始化今日日程');
			var now = new Date();
			var year = now.getFullYear(); //得到年份
			var month = now.getMonth(); //得到月份
			var date = now.getDate(); //得到日期
			month = month + 1;
			if (month < 10) month = "0" + month;
			if (date < 10) date = "0" + date;
			this.time = year + "-" + month + "-" + date;
			console.log('今天的日期是',this.time);
			uni.request({
			url:"http://127.0.0.1:8080/user/"+this.uid,
			header: {
				Authorization:"Bearer " + uni.getStorageSync('token')
			},
			success: (res) => {
				console.log(res);
				this.name = res.data.name;
				this.mineImage = res.data.picture;
			}
		})
			uni.request({
				url: this.apiUrl + 'schedule',
				method: 'GET',
				header:{
					Authorization: "Bearer " + uni.getStorageSync('token')
				},
				data: {
					uid: this.uid,
					date: this.time
				},
				success: (res) => {
					console.log('请求远程天数据')
					this.dailyScheduleList = []
					this.dailyScheduleList = res.data.records;
					console.log('今日日程有',this.dailyScheduleList)
					uni.setStorageSync(this.uid+'dailySchedule'+this.time,this.dailyScheduleList)
				},
				fail: (err) => {
					console.log('请求本地天数据')
					this.dailyScheduleList = uni.getStorageSync(this.uid+'dailySchedule'+this.time);
					console.log(this.dailyScheduleList)
					
				}
			});
		},
	}
};
</script>

<style lang="scss">
$uni-success: #18bc37 !default;

.container {
	padding: 5px;
	font-size: 14px;
	line-height: 18px;
}

.mb-20 {
	margin-bottom: 20rpx;
}

.ml-20 {
	margin-left: 20rpx;
}

.mr-30 {
	margin-right: 30rpx;
}

.demo-uni-row {
	margin-bottom: 15px;
	display: block;
	text-align: center;
}

.decoration {
	width: 6px;
	height: 6px;
	margin-right: 20px;
	border-radius: 50%;
	background-color: $uni-success;
}

.demo-uni-col {
	height: 36px;
	border-radius: 5px;
}

.text {
	font-size: 10px;
	line-height: 20px;
	color: #9999b3;
	text-align: right;
}

.uni-padding-wrap {
	padding: 0 30px;
}
text {
		display: -webkit-box;
		/*弹性伸缩盒子模型显示*/
		-webkit-box-orient: vertical;
		/*排列方式*/
		-webkit-line-clamp: 1;
		/*显示文本行数(这里控制多少行隐藏)*/
		overflow: hidden;
		/*溢出隐藏*/
	}

	navigator:hover {
		/* 当鼠标放到该位置时触发 */
	}

	/* 个人主页信息模块 */
	.my-info {
		display: flex;
		flex-direction: row;
		justify-content: center;
		/* 固定盒子 */
		position: relative;
		margin-top: 5%;
		/* 固定盒子居中方法 */
		left: 50%;
		/* 兼容老版本谷歌浏览器 */
		-webkit-transform: translateX(-50%);
		transform: translateX(-50%);
		/* 固定盒子要有宽度 */
		width: 90%;
		/* min-width: 300px;
		max-width: 540px; */
		/* 高度固定 */
		height: 80px;
		background-color: #55aaff;
		border-radius: 5px 5px 5px 5px;
		z-index: 997;
	}

	.my-info-logo {
		flex: 1;
	}

	.my-info-name {
		margin: 15px auto;
		flex: 2;
	}

	.my-info-change {
		flex: 1;
		margin: 13px auto;
		border-radius: 20px;
		text-align: center;
	}
</style>
